package com.lcq.demo.controller;

import com.google.common.base.Preconditions;
import com.lcq.demo.common.entity.Result;
import com.lcq.demo.common.utils.JWTUtils;
import com.lcq.demo.entity.dto.UserDTO;
import com.lcq.demo.entity.po.User;
import com.lcq.demo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 240701005
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/api/users")
@Slf4j
//@CrossOrigin(origins = "*", allowedHeaders = "token")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     */
    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody UserDTO userDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("UserController.register.userDTO:{}", userDTO);
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(userDTO.getEmail()), "邮箱不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(userDTO.getPassword()), "密码不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(userDTO.getPassword2()), "确认密码不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(userDTO.getVerifyCode()), "验证码不能为空！");
            return Result.ok(userService.register(userDTO));
        } catch (Exception e) {
            log.error("UserController.register.error:{}", e.getMessage(), e);
            return Result.fail("用户注册失败！");
        }
    }

    /**
     * 发送邮箱验证码
     */
    @Operation(summary = "发送邮箱验证码")
    @PostMapping("/sendVerifyCode")
    public Result<String> sendVerifyCode(@RequestBody UserDTO userDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("UserController.sendVerifyCode.userDTO:{}", userDTO);
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(userDTO.getEmail()), "邮箱不能为空！");
            userService.sendEmail(userDTO.getEmail());
            return Result.ok("正在发送验证码~！");
        } catch (Exception e) {
            log.error("UserController.sendVerifyCode.error:{}", e.getMessage(), e);
            return Result.fail("发送验证码失败~！");
        }
    }

    /**
     * 用户登录
     */
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<String> login(@RequestBody UserDTO userDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("UserController.login.userDTO:{}", userDTO);
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(userDTO.getEmail()), "邮箱不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(userDTO.getPassword()), "密码不能为空！");
            User user = userService.login(userDTO);
            Map<String, String> payLoad = new HashMap<>();
            payLoad.put("id", String.valueOf(userDTO.getId()));
            payLoad.put("email", userDTO.getEmail());
            String token = JWTUtils.getToken(payLoad);
            return Result.ok(token);
        } catch (Exception e) {
            log.error("UserController.login.error{}", e.getMessage(), e);
            return Result.fail("用户登录失败！");
        }
    }

}
